#!/bin/bash

show_help() {
cat <<EOF
Usage:

${0##*/} --daemon
    Start Galaxy server as daemon process (paster):
${0##*/} --stop-daemon
    Stop Galaxy daemon process (paster)

Extra Options:

  -m|--mode   How to launch (paster,uwsgi,webless).
              In webless mode Galaxy will not attempt to bind to a port,
              this may be useful for Galaxy server processes acting as job
              handlers.
  -c|--config Configuration directory for Galaxy, defaults to ./config
              Can also set with GALAXY_CONFIG_DIR environment variable.

EOF
}

MODE=""
while :
do
    case "$1" in
      -h|--help|-\?) 
          show_help
          exit 0
          ;;
      -m|--mode)
          if [ $# -gt 1 ]; then
              MODE=$2
              shift 2
          else 
              echo "--mode requires explicit argument" 1>&2
              exit 1
          fi          
          ;;
      -c|--config)
          if [ $# -gt 1 ]; then
              GALAXY_CONFIG_DIR=$2
              shift 2
          else 
              echo "--config requires explicit argument" 1>&2
              exit 1
          fi
          ;;
      --) 
          shift
          break
          ;;
      *)
          break;
          ;;
    esac
done

SCRIPTS_DIRECTORY="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
PROJECT_DIRECTORY=$SCRIPTS_DIRECTORY/..

GALAXY_CONFIG_DIR=${GALAXY_CONFIG_DIR:-"./config"}
export GALAXY_CONFIG_DIR

GALAXY_LOCAL_ENV=${GALAXY_LOCAL_ENV:-$GALAXY_CONFIG_DIR/local_env.sh}
export GALAXY_LOCAL_ENV

GALAXY_VIRTUALENV=${GALAXY_VIRTUALENV:-$PROJECT_DIRECTORY/.venv}
export GALAXY_VIRTUALENV

if [ -e $GALAXY_LOCAL_ENV ];
then
    echo "Sourcing file $GALAXY_LOCAL_ENV"
    . $GALAXY_LOCAL_ENV
fi

if [ -d $GALAXY_VIRTUALENV ]; 
then
    echo "Activating virtual environment $GALAXY_VIRTUALENV/bin/activate"
    . $GALAXY_VIRTUALENV/bin/activate
fi

GALAXY_CONFIG_SAMPLE_FILE="${PROJECT_DIRECTORY}/galaxy.ini.sample"
if [ -z "$GALAXY_CONFIG_FILE" ]; then
    if [ -f "${GALAXY_CONFIG_DIR}/galaxy.ini" ]; then
        GALAXY_CONFIG_FILE="${GALAXY_CONFIG_DIR}/galaxy.ini"
    else
        GALAXY_CONFIG_FILE="$GALAXY_CONFIG_SAMPLE_FILE"
    fi
    export GALAXY_CONFIG_FILE
fi

if [ -z "$MODE" ]; 
then
    if hash uwsgi 2>/dev/null; then
        MODE="uwsgi"
    else
        MODE="paster"
    fi
fi

if [ "$MODE" == "uwsgi" ]; then
    echo "Starting galaxy with command [uwsgi --ini-paste \"$GALAXY_CONFIG_FILE\" \"$@\"]"
    uwsgi --ini-paste "$GALAXY_CONFIG_FILE" "$@"
elif [ "$MODE" == "paster" ]; then
    echo "Starting galaxy with command [galaxy-paster serve \"$GALAXY_CONFIG_FILE\" \"$@\"]"
    galaxy-paster serve "$GALAXY_CONFIG_FILE" "$@"
elif [ "$MODE" == "webless" ]; then
    if hash galaxy-main 2>/dev/null; then
        # Running from pip install since
        # galaxy-main is available on PATH.
        echo "Starting galaxy with command [galaxy-main]"
        galaxy-main
    else
        # Running locally.
        PROJECT_DIRECTORY=$GALAXY_SCRIPTS_DIR/..
        cd $PROJECT_DIRECTORY
        export PYTHONPATH=.:$PYTHONPATH
        echo "Starting galaxy with command [python lib/galaxy/main.py]"
        python lib/pulsar/main.py
    fi
else
    echo "Unknown mode passed to --mode argument." 1>&2
    exit 1
fi
